package entity;

public class ETimeRegister extends ERegister {
	private String value = "";
	
	public ETimeRegister(String name, int address, String dataType,
			String description, String unit) {
		super(name, address, dataType, description, false, unit);
	}

	@Override
	public Object getValue() {
		return value;
	}

	@Override
	public void setValue(byte b0, byte b1, byte b2, byte b3) {
		String registerString = "";
		
		String byteString = Integer.toBinaryString(b3);
		if (byteString.length() > 8) {
			registerString += byteString.substring(byteString.length()-8);
		} else {
			registerString += "00000000".substring(byteString.length()) + byteString;
		}
		byteString = Integer.toBinaryString(b2);
		if (byteString.length() > 8) {
			registerString += byteString.substring(byteString.length()-8);
		} else {
			registerString += "00000000".substring(byteString.length()) + byteString;
		}
		byteString = Integer.toBinaryString(b1);
		if (byteString.length() > 8) {
			registerString += byteString.substring(byteString.length()-8);
		} else {
			registerString += "00000000".substring(byteString.length()) + byteString;
		}
		byteString = Integer.toBinaryString(b0);
		if (byteString.length() > 8) {
			registerString += byteString.substring(byteString.length()-8);
		} else {
			registerString += "00000000".substring(byteString.length()) + byteString;
		}
		
		int hour = Integer.parseInt(registerString.substring(0,registerString.length()-24),2);
		int minute = Integer.parseInt(registerString.substring(registerString.length()-24,registerString.length()-16),2);;
		int second = Integer.parseInt(registerString.substring(registerString.length()-16,registerString.length()-8),2);
		
		String time = (hour<10) ? "0" + hour : "" + hour;
		time += (minute<10) ? ":0" + minute : ":" + minute;
		time += (second<10) ? ":0" + second : ":" + second;
		
		if (!time.equalsIgnoreCase(this.value)) {
			value = time;
			
			setChanged();
			notifyObservers(value);
		}
	}

}